package com.example.administrator.myapplication.register;

import android.app.Dialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.NumberPicker;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.myapplication.HomeActivity;
import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.login.LoginActivity;
import com.example.administrator.myapplication.tools.TestData;


import java.util.Timer;
import java.util.TimerTask;


import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;


/**
 * Created by Administrator on 2017/11/7.
 */

public class RegisterFragment extends Fragment{

    private String sex_saver;                    //用来存储用户选择了什么
    private EditText register_username;
    private EditText register_password;
    //    private EditText register_mobilePhoneNumber;
    private RadioGroup register_radiogroup;
    private EditText register_pwd_twice;
    //    private EditText register_userVerify;     //验证码
    private Button register_bt;
    private EditText et_register_age;
    private EditText et_register_weight;
    private EditText et_register_height;
    private TextView et_register_login;
    private TextView et_register_visit;

    private LayoutInflater factory;
    private View myView;
    private int register_height = 0;
    private int register_weight = 0;
    private int register_age = 0;
    private int numSaver;
    private int flag;
    private boolean swich;
    private NumberPicker numberpicker;






    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        final View view=inflater.inflate(R.layout.fragment_register,container,false);
        et_register_login = (TextView) view.findViewById(R.id.register_to_login_tv);
        et_register_visit = (TextView) view.findViewById(R.id.register_visit);

        et_register_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),LoginActivity.class);
                startActivity(intent);
            }
        });
        et_register_visit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), HomeActivity.class);
                startActivity(intent);
            }
        });


        init(view);

        sex_saver="0";                //默认为男
        register_radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                //当选中的是男时
                if(group.getCheckedRadioButtonId()==R.id.rb_register_man){
                    sex_saver="0";         //0代表男
                }else if(group.getCheckedRadioButtonId()==R.id.rb_register_woman){
                    sex_saver="1";
                }
            }
        });

        //设置点击事件





        //创建数据库表
        final TestData bu = new TestData();
        register_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //用户名应该为
                String username =register_username.getText().toString();
                String reg="\\w{6,16}";
                Boolean flag_user=username.matches(reg);
                if(!flag_user){
                    Toast.makeText(getActivity(),"请正确输入6~16位的用户名",Toast.LENGTH_SHORT).show();
                }else if(!register_password.getText().toString().equals(register_pwd_twice.getText().toString())){
                    Toast.makeText(getActivity(),"密码两次输入不相同",Toast.LENGTH_SHORT).show();
                }
                else{
                    bu.setUsername(username);
                    bu.setPassword(register_password.getText().toString());
                    bu.setSex(sex_saver);
                    bu.setHeight(et_register_height.getText().toString());
                    bu.setWeight(et_register_weight.getText().toString());
                    bu.setAge(et_register_age.getText().toString());
//                   bu.setMobilePhoneNumber(register_mobilePhoneNumber.getText().toString());
//                    bu.setVerifynumber(register_userVerify.getText().toString());

                    bu.signUp(new SaveListener<TestData>() {
                        public void done(TestData s, BmobException e) {
                            if(e==null){
                                Toast.makeText(getActivity(),"注册成功",Toast.LENGTH_SHORT).show();
                                //设置一个定时器 ，两秒之后执行以下代码
                                Timer timer =new Timer();
                                final Intent intent =new Intent(getActivity(), LoginActivity.class);
                                timer.schedule(new TimerTask() {
                                    @Override
                                    public void run() {
                                        startActivity(intent);   //两秒之后执行以下代码
                                    }
                                },2000);
                            }else{
                                if(e.getErrorCode()==301){
                                    Toast.makeText(getActivity(),"请输入正确的手机号",Toast.LENGTH_SHORT).show();
                                }else if(e.getErrorCode()==202){
                                    Toast.makeText(getActivity(),"用户名已经存在",Toast.LENGTH_SHORT).show();
                                }else if(e.getErrorCode()==304){
                                    Toast.makeText(getActivity(),"用户名或密码为空",Toast.LENGTH_SHORT).show();
                                }
                                e.printStackTrace();
                                // Toast.makeText(getActivity(),"用户名已存在",Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
            }
        });

        return view;
    }

    public void init(View view){
        register_username=(EditText) view.findViewById(R.id.register_user_et);
        register_password=(EditText) view.findViewById(R.id.register_pwd_et);
//        register_mobilePhoneNumber=(EditText) view.findViewById(R.id.register_phone_et);
//        register_userVerify=(EditText) view.findViewById(R.id.register_verify_et);
        register_radiogroup= (RadioGroup) view.findViewById(R.id.rg_register_sex);
        register_bt=(Button)view.findViewById(R.id.register_bt);
        register_pwd_twice= (EditText) view.findViewById(R.id.register_pwd_twice);

        et_register_age= (EditText) view.findViewById(R.id.et_register_age);
        et_register_height= (EditText) view.findViewById(R.id.et_register_height);
        et_register_weight= (EditText) view.findViewById(R.id.et_register_weight);

    }


}
